//
// Created by guasen on 2022/5/8.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mailUtil.h"
#include <stdint.h>
/*
 * 尝试读出txt文件内容，如果是txt文件则返回文件内容的指针
 * 无则返回空指针
 * */
char *getTextFromFile(const char *fileName){
    const char *pFile;
//   文件不含后缀名直接返回
    pFile = strrchr(fileName, '.'); // 判断输入的文件名最后输出.的位置
    if (pFile == NULL){
        return NULL;
    }
//  txt格式文件读取
    if (strcmp(pFile, ".txt") == 0){
        FILE * r=fopen(fileName,"r");
//        文件不存在
        if (r == NULL){
            return NULL;
        }
        char *textBuf = malloc(INT16_MAX);
        char *readBuf = malloc(4096);
        while(fscanf(r,"%s",readBuf)!=EOF)
        {
            printf("%s ",readBuf);
            printf(textBuf,"%s ",readBuf);//输出到缓冲区
            memset(readBuf,4096,'0');
        }
        return textBuf;
    }
}

char * getFileAsChar(const char *fileName){
    FILE * r=fopen(fileName,"r");
//        文件不存在
    if (r == NULL){
        return NULL;
    }
    char *textBuf = malloc(INT16_MAX);
    char *readBuf = malloc(2999);
    fgets(readBuf, 100, r);
    strcpy(textBuf,readBuf);
     while(fgets(readBuf, 100, r) != NULL){
        printf("%s\n", readBuf);
        strcat(textBuf,readBuf);
    }
    // printf(textBuf,"%c",'\0');
    // printf("%s\n",textBuf);
    free(readBuf);
    return textBuf;
}
char * getFileAsBase64(const char *fileName){
    // int *length;
    // char * data;
    // FILE *guaFile = fopen(fileName,"r");
    // if (guaFile == NULL){
    //     printf("NULL");
    //     return 0;
    // }
    // fseek(guaFile, 0, SEEK_END);
	// *length = ftell(guaFile);
	// data = (char *)malloc((*length + 1) * sizeof(char));
	// rewind(guaFile);
	// *length = fread(data, 1, *length, guaFile);
	// data[*length] = '\0';
    // // printf("%s",data);

    char * data;
    data = getFileAsChar(fileName);
    return encode_str(data);
}



